#include "Date.h"

bool Date::operator<(const Date &d) const
{
    if (m_year < d.m_year)
    {
        return true;
    }
    else if (m_year == d.m_year && m_month < d.m_month)
    {
        return true;
    }
    else if (m_year == d.m_year && m_month == d.m_month && m_day < d.m_day)
    {
        return true;
    }
    else
    {
        return false;
    }
}

Date &Date::operator+=(int day)
{
    m_day += day;
    int days = getMonthDay(m_year, m_month);
    while (m_day > days)
    {
        days = getMonthDay(m_year, m_month);
        m_day -= days;
        m_month++;
        if (m_month == 13)
        {
            m_year++;
            m_month = 1;
        }
    }
    return *this;
}

Date &Date::operator-=(int day)
{
    m_day -= day;

    while (m_day < 0)
    {
        m_month--;
        if (m_month == 0)
        {
            m_month = 12;
            m_year--;
        }
        int days = getMonthDay(m_year, m_month);
        m_day += days;
    }
    return *this;
}

int Date::operator-(const Date &d) const
{
    int count = 0;
    Date max = *this > d ? *this : d;
    Date min = *this < d ? *this : d;
    while (min != max)
    {
        min += 1;
        count++;
    }
    return count;
}

std::istream& operator>>(std::istream &in, Date &d)
{
    in >> d.m_year >> d.m_month >> d.m_day;
    if(d.m_month < 1 || d.m_month > 12)
    {
        cout << "输入的month有误" << endl;
        in.setstate(std::ios::failbit);
        return in;
    }
    int days = d.getMonthDay(d.m_year, d.m_month);
    if(d.m_day > days || d.m_day < 1)
    {
        cout << "输入的day有误" << endl;
        in.setstate(std::ios::failbit);
        return in;
    }
    return in;
}

std::ostream& operator<<(std::ostream &out, Date &d)
{
    out << d.m_year << "-" << d.m_month << "-" << d.m_day << endl;
    return out;
}
